/**
 * 
 */
package fr.ema.arch3d.gea.mainGUI.listeners;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import org.jgraph.JGraph;

import fr.ema.arch3d.gea.mainGUI.archExplorer.ArchTree;

/**
 * class description
 * 
 * @version    version 18 sept. 2009
 * @author     Huaxi (Yulin) ZHANG
 */
/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG    
 */
public class ArchTreeSelectionListener implements TreeSelectionListener {
    static ArchTree archTree;
    static JScrollPane graphPane;
    static JTabbedPane editorTabbedPane;
    static JGraph graphS, graphC, graphA;
    
    /**
     * Constructs...
     *
     *@param archTree
     *@param graphPane
     *@param graphS
     *@param graphC
     *@param graphA
     */
    public ArchTreeSelectionListener(ArchTree archTree, JTabbedPane editorTabbedPane, JGraph graphS,
	JGraph graphC, JGraph graphA){
	this.archTree=archTree;
	this.editorTabbedPane=editorTabbedPane;

	this.graphPane=(JScrollPane) ((JSplitPane) editorTabbedPane.getComponent(0)).getComponent(0);
	this.graphA=graphA;
	this.graphC=graphC;
	this.graphS=graphS;
	
    }
    public void valueChanged(TreeSelectionEvent arg0) {
	editorTabbedPane.setSelectedIndex(0);
        // find select node in the tree
        TreePath path = archTree.getSelectionPath();

        if (path == null) {
            return;
        }

        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) path.getLastPathComponent();

        // define three structure nodes
        DefaultMutableTreeNode specNode   = new DefaultMutableTreeNode();
        DefaultMutableTreeNode configNode = new DefaultMutableTreeNode();
        DefaultMutableTreeNode assNode    = new DefaultMutableTreeNode();

        // find these nodes in the tree
        for (int i = 0; i < archTree.getModel().getChildCount(archTree.getModel().getRoot()); i++) {
            DefaultMutableTreeNode tn =
                (DefaultMutableTreeNode) archTree.getModel().getChild(archTree.getModel().getRoot(), i);

            if (tn.getUserObject().equals("Specification")) {
                specNode = tn;
            } else if (tn.getUserObject().equals("Configuration")) {
                configNode = tn;
            } else if (tn.getUserObject().equals("Assembly")) {
                assNode = tn;
            }
        }

        // active the correspondent graph cell to selection
        if (selectedNode.isNodeAncestor(specNode)) {
            JViewport jvp = (JViewport) graphPane.getComponent(0);
            jvp.setView(graphS);

            // set synchronize selection with graph
            if (selectedNode.isLeaf()) {
                for (int i = 0; i < graphS.getModel().getRootCount(); i++) {
                    if (graphS.getModel().getRootAt(i).toString() != null) {
                        if (graphS.getModel().getRootAt(i).toString().startsWith(
                                (String) selectedNode.getUserObject())) {
                            graphS.setSelectionCell(graphS.getModel().getRootAt(i));
                        }
                    }
                }
            }

            graphS.addGraphSelectionListener(new SyncGraphSelectionListener(archTree, graphPane, graphS, graphC, graphA));
        }

        // active the correspondent graph cell to selection
        else if (selectedNode.isNodeAncestor(configNode)) {
            JViewport jvp = (JViewport) graphPane.getComponent(0);

            jvp.setView(graphC);

            // set synchronize selection with graph
            if (selectedNode.isLeaf()) {
                for (int i = 0; i < graphC.getModel().getRootCount(); i++) {
                    if (graphC.getModel().getRootAt(i).toString() != null) {
                        if (graphC.getModel().getRootAt(i).toString().startsWith(
                                (String) selectedNode.getUserObject())) {
                            graphC.setSelectionCell(graphC.getModel().getRootAt(i));
                        }
                    }
                }
            }

            graphC.addGraphSelectionListener(new SyncGraphSelectionListener(archTree, graphPane, graphS, graphC, graphA));
        }

        // active the correspondent graph cell to selection
        else if (selectedNode.isNodeAncestor(assNode)) {
            JViewport jvp = (JViewport) graphPane.getComponent(0);

            jvp.setView(graphA);

            // set synchronize selection with graph
            if (selectedNode.isLeaf()) {
                for (int i = 0; i < graphA.getModel().getRootCount(); i++) {
                    if (graphA.getModel().getRootAt(i).toString() != null) {
                        if (graphA.getModel().getRootAt(i).toString().startsWith(
                                (String) selectedNode.getUserObject())) {
                            graphA.setSelectionCell(graphA.getModel().getRootAt(i));
                        }
                    }
                }
            }

            graphA.addGraphSelectionListener(new SyncGraphSelectionListener(archTree, graphPane, graphS, graphC, graphA));
        }

        // define the graph selection listener
    }
}

